export default{
    data(){
        return{
            ids:[],
            list:[],
            tolPage:'',
            id:'',
            set:[2,4,6,10],
            size:4,
            select:[],
            modal1: false,
            valuein:'',
            filter:{
                page:1,
                limit:4,
                name:'',
                total:0,
                current:1,
                postId:''
            }
        }
    },
    methods:{
        
        getData(){
            if(this.id){
                console.log('有')
                this.filter.postId=this.id;
                this.$http({
                    method:'post',
                    url:`http://localhost:3000/${this.module}/getcate`,
                    data:this.filter
                })
                .then(res=>{
                    console.log(res);
                    if(res.data.rows){
                        this.list = res.data.rows;
                    }else{
                        this.list = res.data
                    }
                    // this.list = res.data;
                    this.filter.total = res.data.total;
                    this.tolPage=res.data.pages;
                    this.filter.limit=res.data.limit;
                })
            }else{
                console.log('无')//请求分页；
                this.$http({ 
                    method:'post',
                    url:`http://localhost:3000/${this.module}/list`,
                    data:this.filter
                })
                .then(res=>{
                    console.log(res)
                    if(res.data.rows){
                        this.list = res.data.rows;
                    }else{
                        this.list = res.data
                    }
                    this.filter.total = res.data.total;
                    this.tolPage=res.data.pages;
                    this.filter.limit=res.data.limit;
                })
            }
            
        },
        pageSizeChange(page){
            this.size=page;
            this.filter.limit=page;
            
            this.changePage(this.filter.current);
        },
        selected(selection){
            
            this.select=[];
            for(var i=0;i<selection.length;i++){
                this.select.push(selection[i]._id);
            }
        },
        addData(){
            if(this.id){
                this.modal1=true;
                `${this.cleanInput()}`;
                this.itid='';
            }else if(this.module=='users'){//判断如果是用户模块进行操作，就不用进行节点id是否存在的判断
                this.modal1=true;
                `${this.cleanInput()}`;
                this.itid='';
            }else{
                this.$Message.info('请先选择一个分类节点再进行添加');
            } 
        },
        editData(id){
            this.$http({
                method:"GET",
                url:`http://localhost:3000/${this.module}/data/`+id,
            }).then((response)=>{
                `${this.getInput(response)}`;
            })
            
        },
        deleteData(id){
            this.$http({
                method:"delete",
                url:`http://localhost:3000/${this.module}/data/`+id,
            }).then((response)=>{
                // this.getData();
                this.changePage(this.tolPage)
            })
        },
         delAll(){
            this.$http({
                method:"delete",
                url:`http://localhost:3000/${this.module}/data`,
                data:{ids:this.select}
            }).then((response)=>{
                this.$Message.success('删除成功!');
                this.getData();
                this.changePage(this.tolPage);
            })
            
        },
         ok () {
            if(this.module=='users'){
                if(this.formValidate.name.length<=1||this.formValidate.password.length<=2||this.formValidate.email==''){
                    this.$Message.error('基本信息未按要求填写!');
                }else{
                    this.addAndEdit();
                }
            }else{
                this.addAndEdit();
            }
            
        },
        addAndEdit(){
            console.log(this.itid)
            if(this.itid){//判断itid是否存在，如果存在则执行修改操作，如果不存在，则执行添加操作
                console.log('请求')
                this.$http({//修改
                    method:'post',
                    url:`http://localhost:3000/${this.module}/data/`+this.itid,
                    data:this.formValidate
                }).then(res=>{
                    console.log(res);
                    this.$Message.success('修改成功!');
                    this.getData();
                    this.changePage(this.filter.page);
                })
            }else{
                this.$http({//添加
                    method:"post",
                    url:`http://localhost:3000/${this.module}/data`,
                    data:this.formValidate
                }).then((res)=>{
                    this.$Message.success('添加成功!');
                    this.getData();
                    this.changePage(this.tolPage);
                })
            }
        },
        cancel () {
            this.$Message.info('点击了取消');
        },
        search(){
            this.$http({//添加
                method:"post",
                url:`http://localhost:3000/${this.module}/search`,
                data:{cateId:this.id,title:this.valuein,name:this.valuein,comment:this.valuein,productname:this.valuein}
            }).then((response)=>{
                console.log(response)
                this.filter.total=response.data.total
                this.list=response.data.rows;
            })

        },
        recovery(){
            this.getData();
            this.changePage(this.filter.page);
        },
        changePage(page){
            this.filter.page=page;
            this.getData();
            this.filter.current=page;
        }

    }
    
}